package cn.doitedu.rtmk.demo4;

import cn.doitedu.rtmk.beans.UserEvent;
import cn.doitedu.rtmk.interfaces.IRuleCalculatorV1;
import cn.doitedu.rtmk.utils.RuleHitResultBuilder;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.util.Collector;
import org.roaringbitmap.RoaringBitmap;

public class RuleCalculator_1V1 implements IRuleCalculatorV1 {
    RoaringBitmap preSelectUsersBitmap;
    @Override
    public void open(RoaringBitmap preSelectUsersBitmap) {
        this.preSelectUsersBitmap = preSelectUsersBitmap;
    }

    @Override
    public void calc(UserEvent userEvent, Collector<JSONObject> out) {
        // 先判断该用户是否是本规则的目标人群
        if (preSelectUsersBitmap.contains((int) userEvent.getUser_id())) {
            // 判断该行为是否是本规则的触发条件
            if (userEvent.getEvent_id().equals("page_load") && userEvent.getProperties().get("url").equals("/page/001")) {
                // 如果是，则推送消息
                JSONObject resultObj = new RuleHitResultBuilder()
                        .setUser(userEvent.getUser_id())
                        .setHitTime(userEvent.getEvent_time())
                        .setHitRule(1)
                        .build();

                out.collect(resultObj);
            }
        }
    }
}
